package com.zhongyouyun.admin.service.imp;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.pattern.CronPattern;
import cn.hutool.cron.task.CronTask;
import com.zhongyouyun.admin.model.JobModel;
import com.zhongyouyun.admin.mapper.JobMapper;
import com.zhongyouyun.admin.pojo.JobPojo;
import com.zhongyouyun.admin.service.JobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.other.task.BaseTask;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * @Author: shenyi
 * @Date: 2022-12-16
 * @Description: 服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class JobServiceImpl extends ServiceImpl<JobMapper, JobModel> implements JobService {
    @Autowired
    private BaseTask baseTask;

    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<JobModel> selectPage(Page<JobModel> page, QueryWrapper<JobModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<JobModel> selectList(QueryWrapper<JobModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<JobPojo> selectPojoPage(Page page, QueryWrapper<JobPojo> queryWrapper) {
        return this.baseMapper.selectPojoPage(page, queryWrapper);
    }

    @Override
    public Boolean saveforUpdate(JobModel jobModel) {
        Boolean flag=false;
        if (ObjectUtil.isNotEmpty(jobModel.getId())) {
            JobModel oldJob = getById(jobModel.getId());
            if ("1".equals(jobModel.getIsStart())) {
                baseTask.alterJob(oldJob, jobModel);
                flag=true;
            } else {
                baseTask.pauseJob(jobModel);
                flag=true;
            }
        } else {
            saveOrUpdate(jobModel);
            baseTask.addJob(jobModel);
            flag=true;
        }
        this.saveOrUpdate(jobModel);
        return flag;
    }

    @Override
    public Boolean removeforByIds(List<Long> list) {
        return null;
    }
}
